﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HAMath
{

    /// <summary>
    /// 点的信息
    /// </summary>
    [Serializable ]
    public class RefinePoint:ICloneable 
    {

        public RefinePoint()
        {
            this.BaseSize = 5.0;
            this.Bisa = 2.0;
            this.MaxSize = 20.0;
            this.RefinePointCoor.XCoordinate = 10.199002217295;
            this.RefinePointCoor.YCoordinate = 65.560975609756;
            this.RefinePointCoor.ZCoordinate = 0.0;
        
        }


        PointCoordinate _refinePointCoor = new PointCoordinate();

        public PointCoordinate RefinePointCoor
        {
            get { return _refinePointCoor; }
            set { _refinePointCoor = value; }
        }
       

        double _baseSize;

        public double BaseSize
        {
            get { return _baseSize; }
            set { _baseSize = value; }
        }


        double _bisa;

        public double Bisa
        {
            get { return _bisa; }
            set { _bisa = value; }
        }



        double _maxSize;

        public double MaxSize
        {
            get { return _maxSize; }
            set { _maxSize = value; }
        }







        #region ICloneable 成员

        public object Clone()
        {
            RefinePoint obj = new RefinePoint();

            obj = (RefinePoint)this.MemberwiseClone();

            PointCoordinate pointCoor = new PointCoordinate();
            pointCoor = (PointCoordinate)this._refinePointCoor.Clone();

            obj._refinePointCoor = pointCoor;

            return obj;

        }

        #endregion


    }
    /// <summary>
    /// 点所在的网格信息
    /// </summary>
     [Serializable]
    public class GridFrameParameter:ICloneable 
    {

        public GridFrameParameter()
        {
            this.RotateZ = 0.0;
            this.XWidth = 121.0;
            this.YHeight = 67.0;

            this.LowLeft.XCoordinate = -17;
            this.LowLeft.YCoordinate = 16;
            this.LowLeft.ZCoordinate = 0;

            this.LowRight.XCoordinate = 104;
            this.LowRight.YCoordinate = 16;
            this.LowRight.ZCoordinate = 0;

            this.UpLeft.XCoordinate = -17;
            this.UpLeft.YCoordinate = 82;
            this.UpLeft.ZCoordinate = 0;


            this.UpRight.XCoordinate = 104;
            this.UpRight.YCoordinate = 82;
            this.UpRight.ZCoordinate = 0;

        
        }



        double _rotateZ=0.0;

        public double RotateZ
        {
            get { return _rotateZ; }
            set { _rotateZ = value; }
        }


        double _xWidth=121.0;

        public double XWidth
        {
            get { return _xWidth; }
            set { _xWidth = value; }
        }

        double _yHeight=67.0;

        public double YHeight
        {
            get { return _yHeight; }
            set { _yHeight = value; }
        }


        PointCoordinate _lowLeft = new PointCoordinate();

        public PointCoordinate LowLeft
        {
            get { return _lowLeft; }
            set { _lowLeft = value; }
        }


        PointCoordinate _lowRight = new PointCoordinate();

        public PointCoordinate LowRight
        {
            get { return _lowRight; }
            set { _lowRight = value; }
        }

        PointCoordinate _upLeft = new PointCoordinate();

        public PointCoordinate UpLeft
        {
            get { return _upLeft; }
            set { _upLeft = value; }
        }

        PointCoordinate _upRight = new PointCoordinate();

        public PointCoordinate UpRight
        {
            get { return _upRight; }
            set { _upRight = value; }
        }


        public  void CoorChanged()
        { 
        
            //更行  Width 和 Height

            this._xWidth = this.LowRight.XCoordinate  - this.LowLeft.XCoordinate ;
            this._yHeight = this._upLeft.YCoordinate  - this.LowLeft.YCoordinate ;
        
        }




        #region ICloneable 成员

        public object Clone()
        {
            GridFrameParameter obj = new GridFrameParameter();
            obj = (GridFrameParameter)this.MemberwiseClone();


            PointCoordinate lowLeft = new PointCoordinate();
            lowLeft = (PointCoordinate)this.LowLeft.Clone();
            obj.LowLeft = lowLeft;

            PointCoordinate lowRight = new PointCoordinate();
            lowRight = (PointCoordinate)this.LowRight .Clone();
            obj.LowRight  = lowRight;

            PointCoordinate lupLeft = new PointCoordinate();
            lupLeft = (PointCoordinate)this.UpLeft .Clone();
            obj.UpLeft  = lupLeft;

            PointCoordinate upRight = new PointCoordinate();
            upRight = (PointCoordinate)this.UpRight .Clone();
            obj.UpRight  = upRight;

            return obj;

        }

        #endregion
    }
    /// <summary>
    /// 坐标
    /// </summary>
   [Serializable ]
    public class PointCoordinate:ICloneable 
    {

        double _xCoordinate;

        public double XCoordinate
        {
            get { return _xCoordinate; }
            set { _xCoordinate = value; }
        }

        double _yCoordinate;

        public double YCoordinate
        {
            get { return _yCoordinate; }
            set { _yCoordinate = value; }
        }


        double _zCoordinate;

        public double ZCoordinate
        {
            get { return _zCoordinate; }
            set { _zCoordinate = value; }
        }




        #region ICloneable 成员

        public object Clone()
        {
            PointCoordinate obj = new PointCoordinate();

            obj = (PointCoordinate)this.MemberwiseClone();

            return obj;


        }

        #endregion
    }

   /// <summary>
   /// 返回结果
   /// </summary>
    public class XYCoorResult
    {

        public XYCoorResult()
        {

            this.XCoor = new List<double>();

            this.YCoor = new List<double>();
        
        }


        List<double> _xCoor;

        public List<double> XCoor
        {
            get { return _xCoor; }
            set { _xCoor = value; }
        }

        List<double> _yCoor;

        public List<double> YCoor
        {
            get { return _yCoor; }
            set { _yCoor = value; }
        }



    }


    /// <summary>
    /// 计算取得要构建的网格
    /// </summary>
    public class CreateGridFromRefinePoints
    {





        /// <summary>
        /// 返回 XYCoorResult result  出错时result中包含XY坐标的两个 List 的 Count=0
        /// </summary>
        /// <param name="gridFrameParamter"></param>
        /// <param name="refinePoint"></param>
        public static XYCoorResult CreateGridRefinePoints(GridFrameParameter gridFrameParamter, List<RefinePoint> refinePoint)
        {
            //注意 所有的坐标  需要统一坐标系  坐标系变换

            //注意平移旋转的处理

            XYCoorResult result = new XYCoorResult();


            if (gridFrameParamter == null || refinePoint == null)
            {
                return result;
            }


            int refinePointNum = refinePoint.Count;

            if (refinePoint .Count <=0)
            {
                 return result;
            }

            try
            {



                //网格边框信息

                GridFrameParameter gridFrame = new GridFrameParameter();

                gridFrame = (GridFrameParameter)gridFrameParamter.Clone();

                //点1信息及和网格边框的关系

                List<RefinePoint> refinePointList = new List<RefinePoint>();

                for (int i = 0; i < refinePoint.Count; i++)
                {
                    RefinePoint rePoint;
                    rePoint = (RefinePoint)refinePoint[i].Clone();
                    refinePointList.Add(rePoint);

                }


                #region 缺乏一些数据检验的信息
                //比如网格数据是否正确：如lowRight>lowLeft等

                #endregion



                if (refinePointNum <= 0)
                {
                    return result;

                }
                else if (refinePointNum == 1)
                {
                    List<double> xCoordinate;

                    xCoordinate = CreateGridOneRefinePointsXCoor(gridFrame, refinePointList[0]);

                    List<double> yCoordinate;
                    yCoordinate = CreateGridOneRefinePointsYCoor(gridFrame, refinePointList[0]);


                    //整理返回的网格坐标结果

                    #region


                    if (xCoordinate == null || yCoordinate == null)
                    {

                        result = new XYCoorResult();
                        return result;
                    }
                    else
                    {
                        if (xCoordinate.Count <= 0 || yCoordinate.Count <= 0)
                        {
                            result = new XYCoorResult();
                            return result;
                        }
                    }
                    //存储结果
                    if (result == null)
                    {
                        result = new XYCoorResult();
                    }
                    for (int i = 0; i < xCoordinate.Count; i++)
                    {
                        result.XCoor.Add(xCoordinate[i]);

                    }
                    for (int j = 0; j < yCoordinate.Count; j++)
                    {
                        result.YCoor.Add(yCoordinate[j]);
                    }

                    #endregion

                    return result;

                }
                else
                {
                    #region

                    //点个数 >=2

                    //首先计算X方向
                    //X方向排序  分割区域

                    List<RefinePoint> xTempPoint;
                    xTempPoint = SortPointXCoor(refinePointList);

                    List<RefinePoint> xRefinePoint = new List<RefinePoint>();//点X方向排序后
                    for (int i = 0; i < xTempPoint.Count; i++)
                    {
                        if (i == 0)
                        {
                            xRefinePoint.Add(xTempPoint[0]);
                        }
                        else
                        {
                            if (xRefinePoint.Count > 0)
                            {
                                if (xTempPoint[i] != xRefinePoint[xRefinePoint.Count - 1])
                                {
                                    xRefinePoint.Add(xTempPoint[i]);
                                }
                            }
                            else
                            {
                                xRefinePoint.Add(xTempPoint[i]);
                            }

                        }

                    }


                    //xRefinePoint = SortPointXCoor(refinePointList);



                    List<double> xCoor = null;

                    //调试伪代码
                    //xRefinePoint[0].RefinePointCoor.XCoordinate = 0.0;



                    #region

                    if (xRefinePoint != null)
                    {
                        //构建 各个点对应的网格  GridFrame

                        xCoor = new List<double>();

                        for (int i = 0; i < xRefinePoint.Count; i++)
                        {
                            GridFrameParameter gridFrameI = new GridFrameParameter();

                            //构建对应于某个点的网格信息

                            #region

                            if (i == 0)
                            {

                                gridFrameI.LowLeft.XCoordinate = gridFrame.LowLeft.XCoordinate;
                                gridFrameI.LowLeft.YCoordinate = gridFrame.LowLeft.YCoordinate;

                                gridFrameI.UpLeft.XCoordinate = gridFrame.UpLeft.XCoordinate;
                                gridFrameI.UpLeft.YCoordinate = gridFrame.UpLeft.YCoordinate;

                                gridFrameI.LowRight.XCoordinate = (xRefinePoint[0].RefinePointCoor.XCoordinate + xRefinePoint[1].RefinePointCoor.XCoordinate) * 0.5;
                                gridFrameI.LowRight.YCoordinate = gridFrame.LowLeft.YCoordinate;

                                gridFrameI.UpRight.XCoordinate = (xRefinePoint[0].RefinePointCoor.XCoordinate + xRefinePoint[1].RefinePointCoor.XCoordinate) * 0.5;
                                gridFrameI.UpRight.YCoordinate = gridFrame.UpLeft.YCoordinate;


                                gridFrameI.CoorChanged();

                            }
                            else if (i == xRefinePoint.Count - 1)
                            {


                                gridFrameI.LowLeft.XCoordinate = (xRefinePoint[i - 1].RefinePointCoor.XCoordinate + xRefinePoint[i].RefinePointCoor.XCoordinate) * 0.5;
                                gridFrameI.LowLeft.YCoordinate = gridFrame.LowLeft.YCoordinate;

                                gridFrameI.UpLeft.XCoordinate = (xRefinePoint[i - 1].RefinePointCoor.XCoordinate + xRefinePoint[i].RefinePointCoor.XCoordinate) * 0.5;
                                gridFrameI.UpLeft.YCoordinate = gridFrame.UpLeft.YCoordinate;

                                gridFrameI.LowRight.XCoordinate = gridFrame.LowRight.XCoordinate;
                                gridFrameI.LowRight.YCoordinate = gridFrame.LowRight.YCoordinate;

                                gridFrameI.UpRight.XCoordinate = gridFrame.UpRight.XCoordinate;
                                gridFrameI.UpRight.YCoordinate = gridFrame.UpRight.YCoordinate;


                                gridFrameI.CoorChanged();


                            }
                            else
                            {

                                gridFrameI.LowLeft.XCoordinate = (xRefinePoint[i - 1].RefinePointCoor.XCoordinate + xRefinePoint[i].RefinePointCoor.XCoordinate) * 0.5;
                                gridFrameI.LowLeft.YCoordinate = gridFrame.LowLeft.YCoordinate;

                                gridFrameI.UpLeft.XCoordinate = (xRefinePoint[i - 1].RefinePointCoor.XCoordinate + xRefinePoint[i].RefinePointCoor.XCoordinate) * 0.5;
                                gridFrameI.UpLeft.YCoordinate = gridFrame.UpLeft.YCoordinate;



                                gridFrameI.LowRight.XCoordinate = (xRefinePoint[i].RefinePointCoor.XCoordinate + xRefinePoint[i + 1].RefinePointCoor.XCoordinate) * 0.5;
                                gridFrameI.LowRight.YCoordinate = gridFrame.LowRight.YCoordinate;

                                gridFrameI.UpRight.XCoordinate = (xRefinePoint[i].RefinePointCoor.XCoordinate + xRefinePoint[i + 1].RefinePointCoor.XCoordinate) * 0.5;
                                gridFrameI.UpRight.YCoordinate = gridFrame.UpRight.YCoordinate;


                                gridFrameI.CoorChanged();

                            }


                            #endregion

                            List<double> xCoordinate = new List<double>();
                            xCoordinate = CreateGridOneRefinePointsXCoor(gridFrameI, xRefinePoint[i]);



                            //整理结果

                            #region

                            if (xCoordinate == null)
                            {
                                result = new XYCoorResult();
                                return result;
                            }
                            else
                            {
                                if (xCoordinate.Count <= 0)
                                {
                                    result = new XYCoorResult();
                                    return result;
                                }

                            }



                            for (int t = 0; t < xCoordinate.Count; t++)
                            {
                                if (xCoor.Count <= 0)
                                {
                                    xCoor.Add(xCoordinate[t]);
                                }
                                else
                                {
                                    //if (xCoordinate[t] > xCoor[xCoor.Count - 1])
                                    if ((xCoordinate[t] - xCoor[xCoor.Count - 1])>1e-6)
                                    {
                                        xCoor.Add(xCoordinate[t]);
                                    }
                                }


                            }


                            #endregion




                        }


                    }

                    #endregion



                    //然后计算Y方向

                    //Y方向排序   分割区域
                    List<RefinePoint> yTempPoint;
                    yTempPoint = SortPointYCoor(refinePointList);

                    List<RefinePoint> yRefinePoint = new List<RefinePoint>();

                    for (int i = 0; i < yTempPoint.Count; i++)
                    {
                        if (i == 0)
                        {
                            yRefinePoint.Add(yTempPoint[0]);
                        }
                        else
                        {
                            if (yRefinePoint.Count > 0)
                            {
                                if (yTempPoint[i] != yRefinePoint[yRefinePoint.Count - 1])
                                {
                                    yRefinePoint.Add(yTempPoint[i]);
                                }
                            }
                            else
                            {
                                yRefinePoint.Add(yTempPoint[i]);
                            }

                        }

                    }
                    //yRefinePoint = SortPointYCoor(refinePointList);

                    List<double> yCoor = null;

                    #region

                    if (yRefinePoint != null)
                    {
                        //构建 各个点对应的网格  GridFrame

                        yCoor = new List<double>();


                        //yRefinePoint中的顺序  按照 y 值 从小到大排列
                        for (int i = 0; i < yRefinePoint.Count; i++)
                        {
                            GridFrameParameter gridFrameI = new GridFrameParameter();

                            //构建对应于某个点的网格信息

                            #region

                            if (i == 0)
                            {

                                gridFrameI.LowLeft.XCoordinate = gridFrame.LowLeft.XCoordinate;
                                gridFrameI.LowLeft.YCoordinate = gridFrame.LowLeft.YCoordinate;

                                gridFrameI.UpLeft.XCoordinate = gridFrame.LowLeft.XCoordinate;
                                gridFrameI.UpLeft.YCoordinate = (yRefinePoint[0].RefinePointCoor.YCoordinate + yRefinePoint[1].RefinePointCoor.YCoordinate) * 0.5;

                                gridFrameI.LowRight.XCoordinate = gridFrame.LowRight.XCoordinate;
                                gridFrameI.LowRight.YCoordinate = gridFrame.LowRight.YCoordinate;

                                gridFrameI.UpRight.XCoordinate = gridFrame.LowRight.XCoordinate;
                                gridFrameI.UpRight.YCoordinate = (yRefinePoint[0].RefinePointCoor.YCoordinate + yRefinePoint[1].RefinePointCoor.YCoordinate) * 0.5;


                                gridFrameI.CoorChanged();

                            }
                            else if (i == yRefinePoint.Count - 1)
                            {


                                gridFrameI.LowLeft.XCoordinate = gridFrame.LowLeft.XCoordinate;
                                gridFrameI.LowLeft.YCoordinate = (yRefinePoint[i - 1].RefinePointCoor.YCoordinate + yRefinePoint[i].RefinePointCoor.YCoordinate) * 0.5;

                                gridFrameI.UpLeft.XCoordinate = gridFrame.UpLeft.XCoordinate;
                                gridFrameI.UpLeft.YCoordinate = gridFrame.UpLeft.YCoordinate;

                                gridFrameI.LowRight.XCoordinate = gridFrame.LowRight.XCoordinate;
                                gridFrameI.LowRight.YCoordinate = (yRefinePoint[i - 1].RefinePointCoor.YCoordinate + yRefinePoint[i].RefinePointCoor.YCoordinate) * 0.5;

                                gridFrameI.UpRight.XCoordinate = gridFrame.UpRight.XCoordinate;
                                gridFrameI.UpRight.YCoordinate = gridFrame.UpRight.YCoordinate;


                                gridFrameI.CoorChanged();


                            }
                            else
                            {

                                gridFrameI.LowLeft.XCoordinate = gridFrame.LowLeft.XCoordinate;
                                gridFrameI.LowLeft.YCoordinate = (yRefinePoint[i - 1].RefinePointCoor.YCoordinate + yRefinePoint[i].RefinePointCoor.YCoordinate) * 0.5;

                                gridFrameI.UpLeft.XCoordinate = gridFrame.UpLeft.XCoordinate;
                                gridFrameI.UpLeft.YCoordinate = (yRefinePoint[i + 1].RefinePointCoor.YCoordinate + yRefinePoint[i].RefinePointCoor.YCoordinate) * 0.5;



                                gridFrameI.LowRight.XCoordinate = gridFrame.LowRight.XCoordinate;
                                gridFrameI.LowRight.YCoordinate = (yRefinePoint[i].RefinePointCoor.YCoordinate + yRefinePoint[i - 1].RefinePointCoor.YCoordinate) * 0.5;

                                gridFrameI.UpRight.XCoordinate = gridFrame.UpRight.XCoordinate;
                                gridFrameI.UpRight.YCoordinate = (yRefinePoint[i].RefinePointCoor.YCoordinate + yRefinePoint[i + 1].RefinePointCoor.YCoordinate) * 0.5;


                                gridFrameI.CoorChanged();

                            }


                            #endregion


                            List<double> yCoordinate = new List<double>();

                            yCoordinate = CreateGridOneRefinePointsYCoor(gridFrameI, yRefinePoint[i]);


                            //整理结果

                            #region

                            if (yCoordinate == null)
                            {
                                result = new XYCoorResult();
                                return result;
                            }
                            else
                            {
                                if (yCoordinate.Count <= 0)
                                {
                                    result = new XYCoorResult();
                                    return result;
                                }

                            }



                            for (int t = 0; t < yCoordinate.Count; t++)
                            {
                                if (yCoor.Count <= 0)
                                {
                                    yCoor.Add(yCoordinate[t]);
                                }
                                else
                                {
                                    //if (yCoordinate[t] > yCoor[yCoor.Count - 1])
                                     if ((yCoordinate[t] -yCoor[yCoor.Count - 1])>1e-6)
                                    {
                                        yCoor.Add(yCoordinate[t]);
                                    }
                                }
                                //yCoor.Add(yCoordinate[t]);

                            }



                            #endregion


                        }// for (int i = 0; i < yRefinePoint.Count; i++)结束




                    }

                    #endregion



                    //整理结果
                    if (xCoor == null || yCoor == null)
                    {
                        result = new XYCoorResult();
                        return result;
                    }
                    else
                    {
                        if (xCoor.Count <= 0 || yCoor.Count <= 0)
                        {
                            result = new XYCoorResult();
                            return result;
                        }
                    }




                    //两个 list 存储数据都是从小到大

                    for (int t = 0; t < xCoor.Count; t++)
                    {
                        result.XCoor.Add(xCoor[t]);
                    }

                    for (int j = 0; j <yCoor .Count ; j++)
                    {
                        result.YCoor.Add(yCoor[j]);
                    }

                    return result;


                    #endregion

                }


                //return result;

            }
            catch
            {
                result = new XYCoorResult();
                return result;
            }

        }


        /// <summary>
        /// 只有一个RefinePoint点的情况  返回 XCoord  出错时  List<double> XCoord .Count=0
        /// </summary>
        public static List<double> CreateGridOneRefinePointsXCoor(GridFrameParameter gridFrame, RefinePoint refinePoint)
        {
            //X坐标按照从左向右的顺序存储



            //说明：首先找到最后一个合理的单元格 限制条件：长度小于MaxSize；已使用距离足够（单元格长度加和）
            //若剩余距离够整数个MaxSize，恰好分；若不是整数个，把最后一个加进来，平均分距离

            List<double> XCoord = new List<double>();

            if (gridFrame == null || refinePoint == null)
            {
                return XCoord;
            }

            try
            {

                #region

                #region 首先定义一些必须的数据信息

                //网格边框信息
                double rotate = gridFrame.RotateZ;
                double XWidth = gridFrame.XWidth;
                double YHeight = gridFrame.YHeight;
                double[] lowleft = new double[3];
                lowleft[0] = gridFrame.LowLeft.XCoordinate;
                lowleft[1] = gridFrame.LowLeft.YCoordinate;
                lowleft[2] = 0;
                double[] upleft = new double[3];
                upleft[0] = gridFrame.UpLeft.XCoordinate;
                upleft[1] = gridFrame.UpLeft.YCoordinate;
                upleft[2] = 0;

                double[] upright = new double[3];
                upright[0] = gridFrame.UpRight.XCoordinate;
                upright[1] = gridFrame.UpRight.YCoordinate;
                upright[2] = 0;
                double[] lowright = new double[3];
                lowright[0] = gridFrame.LowRight.XCoordinate;
                lowright[1] = gridFrame.LowRight.YCoordinate;
                lowright[2] = 0;



                //点1信息及和网格边框的关系
                int refinePointNum = 1;

                double[] refinePoint1XYZ = new double[3];
                refinePoint1XYZ[0] = refinePoint.RefinePointCoor.XCoordinate;
                refinePoint1XYZ[1] = refinePoint.RefinePointCoor.YCoordinate;
                refinePoint1XYZ[2] = 0;

                double p1BaseSize = refinePoint.BaseSize;
                double p1Bisa = refinePoint.Bisa;
                double p1MaxSize = refinePoint.MaxSize;



                double x1ToLeft = (refinePoint1XYZ[0] - lowleft[0]); ;
                double x1ToRight = (lowright[0] - refinePoint1XYZ[0]); ;



                #endregion



                #region 部分参数的有效性检验

                if (p1BaseSize <= 0 || p1MaxSize <= 0)
                {
                    return XCoord;
                }
                if (p1MaxSize < p1BaseSize)
                {
                    return XCoord;
                }

                //该参数先按照简化情况处理

                if (p1Bisa < 0)
                {
                    return XCoord;
                }

                #endregion




                XCoord.Add(lowleft[0]);//左上角第一个点的X坐标  网格边框点  存储顺序从左向右



                //if (p1Bisa > 0)
                {
                    if (p1Bisa < 1)
                    {
                        //暂时按照平分为2

                        List<double> addCellLengthLeft = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengthLeft = 0;//记录上述List中已经添加的cell的长度的总和

                        #region 左侧

                        //平分为2个
                        if (x1ToLeft <= p1BaseSize * 0.5)
                        {
                            //左侧不添加
                        }
                        else
                        {
                            int cellNum = 2;
                            double ableLength = (x1ToLeft - p1BaseSize * 0.5) / (double)cellNum;

                            for (int i = 0; i < cellNum; i++)
                            {
                                addCellLengthLeft.Add(ableLength);
                                addCellTotalLengthLeft = addCellTotalLengthLeft + ableLength;
                            }

                        }


                        int xCoorIndex = XCoord.Count - 1;
                        if (addCellLengthLeft.Count > 0)
                        {

                            for (int i = addCellLengthLeft.Count - 1; i > -1; i--)
                            {
                                XCoord.Add(XCoord[xCoorIndex] + addCellLengthLeft[i]);

                                xCoorIndex = xCoorIndex + 1;

                            }
                        }

                        #endregion

                        List<double> addCellLengthRight = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengthRight = 0;//记录上述List中已经添加的cell的长度的总和

                        #region 右侧

                        //平分为2个

                        if (x1ToRight <= p1BaseSize * 0.5)
                        {
                            //左侧不添加
                        }
                        else
                        {
                            int cellNum = 2;
                            double ableLength = (x1ToRight - p1BaseSize * 0.5) / (double)cellNum;

                            for (int i = 0; i < cellNum; i++)
                            {
                                addCellLengthRight.Add(ableLength);
                                addCellTotalLengthRight = addCellTotalLengthRight + ableLength;
                            }

                        }

                        if (addCellLengthRight.Count > 0)
                        {
                            //首先添加点所在的单元格的右侧坐标

                            XCoord.Add(refinePoint1XYZ[0] + p1BaseSize * 0.5);

                            int cellIndex = XCoord.Count - 1;
                            for (int i = 0; i < addCellLengthRight.Count; i++)
                            {
                                XCoord.Add(XCoord[cellIndex] + addCellLengthRight[i]);
                                cellIndex = cellIndex + 1;
                            }
                        }
                        else
                        {
                            XCoord.Add(lowright[0]);
                        }

                        #endregion

                    }
                    else if (p1Bisa == 1)
                    {

                        #region //先往点的左侧走

                        List<double> addCellLengthLeft = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengthLeft = 0;//记录上述List中已经添加的cell的长度的总和

                        if (x1ToLeft <= p1BaseSize * 0.5)
                        {
                            //左侧不添加
                        }
                        else
                        {
                            double ableLength = x1ToLeft - p1BaseSize * 0.5;

                            int cellNum = (int)Math.Ceiling(ableLength / p1BaseSize);

                            double cellLength = ableLength / (double)cellNum;


                            for (int i = 0; i < cellNum; i++)
                            {
                                addCellLengthLeft.Add(cellLength);
                                addCellTotalLengthLeft = addCellTotalLengthLeft + cellLength;
                            }

                            //for (int i = 1; true; i++)
                            //{
                            //    double averageCellLen = ableLength / i;

                            //    if (averageCellLen <= p1MaxSize)
                            //    {
                            //        for (int index = 0; index < i; index++)
                            //        {
                            //            addCellLengthLeft.Add(averageCellLen);
                            //            addCellTotalLengthLeft = addCellTotalLengthLeft + averageCellLen;
                            //        }
                            //        break;
                            //    }
                            //    else
                            //    {
                            //        continue;
                            //    }

                            //}



                        }



                        #region 添加点的左侧X方向坐标


                        int xCoorIndex = 0;
                        if (addCellLengthLeft.Count > 0)
                        {

                            for (int i = addCellLengthLeft.Count - 1; i > -1; i--)
                            {
                                XCoord.Add(XCoord[xCoorIndex] + addCellLengthLeft[i]);

                                xCoorIndex = xCoorIndex + 1;

                            }
                        }


                        #endregion

                        #endregion


                        #region//再往点的右侧走

                        List<double> addCellLengthRight = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengthRight = 0;//记录上述List中已经添加的cell的长度的总和

                        if (x1ToRight <= p1BaseSize * 0.5)
                        {
                            //右侧不添加
                        }
                        else
                        {

                            double ableLength = x1ToRight - p1BaseSize * 0.5;

                            int cellNum = (int)Math.Ceiling(ableLength / p1BaseSize);

                            double cellLength = ableLength / (double)cellNum;


                            for (int i = 0; i < cellNum; i++)
                            {
                                addCellLengthRight.Add(cellLength);
                                addCellTotalLengthRight = addCellTotalLengthRight + cellLength;
                            }

                            //for (int i = 1; true; i++)
                            //{
                            //    double averageCellLen = ableLength / i;

                            //    if (averageCellLen <= p1MaxSize)
                            //    {
                            //        for (int index = 0; index < i; index++)
                            //        {
                            //            addCellLengthRight.Add(averageCellLen);
                            //            addCellTotalLengthRight = addCellTotalLengthRight + averageCellLen;
                            //        }
                            //        break;
                            //    }
                            //    else
                            //    {
                            //        continue;
                            //    }

                            //}



                        }


                        #region 添加点的右侧X方向坐标


                        if (addCellLengthRight.Count > 0)
                        {
                            //首先添加点所在的单元格的右侧坐标

                            XCoord.Add(refinePoint1XYZ[0] + p1BaseSize * 0.5);
                            int cellIndex = XCoord.Count - 1;

                            for (int i = 0; i < addCellLengthRight.Count; i++)
                            {
                                XCoord.Add(XCoord[cellIndex] + addCellLengthRight[i]);
                                cellIndex = cellIndex + 1;
                            }
                        }
                        else
                        {
                            XCoord.Add(lowright[0]);
                        }


                        #endregion


                        #endregion

                    }
                    else if (p1Bisa > 1)
                    {

                        // 下面是具体的计算网格分布的方法

                        //   //先往点的左侧走

                        List<double> addCellLengthLeft = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengthLeft = 0;//记录上述List中已经添加的cell的长度的总和

                        if (Math.Abs(p1BaseSize - p1MaxSize) < 1e-6)// 认为 p1BaseSize =p1MaxSize 
                        {
                            //平分为2个
                            if (x1ToLeft <= p1BaseSize * 0.5)
                            {
                                //左侧不添加
                            }
                            else
                            {
                                int cellNum = 2;
                                double ableLength = (x1ToLeft - p1BaseSize * 0.5) / (double)cellNum;

                                for (int i = 0; i < cellNum; i++)
                                {
                                    addCellLengthLeft.Add(ableLength);
                                    addCellTotalLengthLeft = addCellTotalLengthLeft + ableLength;
                                }

                            }


                            int xCoorIndex = XCoord.Count - 1;
                            if (addCellLengthLeft.Count > 0)
                            {

                                for (int i = addCellLengthLeft.Count - 1; i > -1; i--)
                                {
                                    XCoord.Add(XCoord[xCoorIndex] + addCellLengthLeft[i]);

                                    xCoorIndex = xCoorIndex + 1;

                                }
                            }


                        }
                        else if (p1BaseSize < p1MaxSize)
                        {
                            #region

                            if (x1ToLeft <= p1BaseSize * 0.5)
                            {
                                //左侧不添加
                            }
                            else
                            {
                                for (int i = 1; true; i++)
                                {

                                    double cellLengthI;//合理的单元格的长度
                                    cellLengthI = Math.Pow(p1Bisa, i) * p1BaseSize;

                                    if (cellLengthI <= p1MaxSize)//cell的Max允许添加
                                    {
                                        //判断剩余距离是否够添加该长度Cell



                                        if (x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft >= cellLengthI)//距离足够
                                        {
                                            addCellLengthLeft.Add(cellLengthI);
                                            addCellTotalLengthLeft = addCellTotalLengthLeft + cellLengthI;
                                        }
                                        else
                                        {
                                            //距离不够添加该cell

                                            addCellLengthLeft.Add(x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft);
                                            addCellTotalLengthLeft = addCellTotalLengthLeft + x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft;

                                            #region 下面的算法已经被上面几句话简化代替掉

                                            //if (addCellLengthLeft.Count > 0)
                                            //{
                                            //    #region

                                            //    if (x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft >= addCellLengthLeft[addCellLengthLeft.Count - 1])
                                            //    {

                                            //        addCellLengthLeft.Add(x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft);
                                            //        addCellTotalLengthLeft = addCellTotalLengthLeft + x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft;


                                            //    }
                                            //    else
                                            //    {

                                            //        addCellTotalLengthLeft = addCellTotalLengthLeft - addCellLengthLeft[addCellLengthLeft.Count - 1];
                                            //        addCellLengthLeft.RemoveAt(addCellLengthLeft.Count - 1);

                                            //        //剩余距离分配

                                            //        #region

                                            //        if (x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft > p1MaxSize)
                                            //        {

                                            //            double lenTotal = x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft;

                                            //            for (int num = 1; true; num++)
                                            //            {
                                            //                //计算分割为几个单元格比较合适

                                            //                double everageCellLen = (double)((decimal)lenTotal / num);

                                            //                if (everageCellLen > p1MaxSize)
                                            //                {
                                            //                    double everage = (double)((decimal)lenTotal / (num + 1));
                                            //                    if (everage <= addCellLengthLeft[addCellLengthLeft.Count - 1])
                                            //                    {
                                            //                        //添加上吧
                                            //                        for (int index = 0; index < num; index++)
                                            //                        {
                                            //                            addCellLengthLeft.Add(everageCellLen);
                                            //                            addCellTotalLengthLeft = addCellTotalLengthLeft + everageCellLen;
                                            //                        }

                                            //                        break;

                                            //                    }
                                            //                    else
                                            //                    {
                                            //                        //继续找
                                            //                        continue;
                                            //                    }

                                            //                }
                                            //                else
                                            //                {
                                            //                    //就这样分配吧  有可能平均分配的长度小于前边有效合理分配的单元格的长度！！！

                                            //                    for (int index = 0; index < num; index++)
                                            //                    {
                                            //                        addCellLengthLeft.Add(everageCellLen);
                                            //                        addCellTotalLengthLeft = addCellTotalLengthLeft + everageCellLen;
                                            //                    }

                                            //                    break;
                                            //                }


                                            //            }// for (int num = 2; true ; num++)  结束


                                            //        }
                                            //        else
                                            //        {
                                            //            //剩余的分配为1个

                                            //            addCellLengthLeft.Add(x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft);
                                            //            addCellTotalLengthLeft = addCellTotalLengthLeft + x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft;

                                            //        }

                                            //        #endregion

                                            //        //break;
                                            //    }

                                            //    break;

                                            //    #endregion


                                            //}
                                            //else
                                            //{
                                            //    //添加的第一个cell  距离不够
                                            //    if (x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft < p1BaseSize)
                                            //    {
                                            //    }
                                            //    else
                                            //    {
                                            //        addCellLengthLeft.Add(x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft);
                                            //        addCellTotalLengthLeft = addCellTotalLengthLeft + x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft;
                                            //    }

                                            //}

                                            #endregion

                                            break;


                                        }


                                    }
                                    else
                                    {
                                        //例如 5,2,20  达到了25   MaxSize不允许



                                        //如果正好能够平均分配整数个MaxSize的单元格， 

                                        double ableLen = (x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft);//剩余距离

                                        int cellNum20 = (int)Math.Ceiling(ableLen % p1MaxSize);//能够按照每个MaxSize正好分配  防止0.25等的情况

                                        if (ableLen >= p1MaxSize && cellNum20 == 0)
                                        {
                                            //平均分配

                                            int number = (int)((x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft) / p1MaxSize);
                                            for (int index = 0; index < number; index++)
                                            {
                                                addCellLengthLeft.Add(p1MaxSize);
                                                addCellTotalLengthLeft = addCellTotalLengthLeft + p1MaxSize;
                                            }

                                            break;
                                        }
                                        else
                                        {

                                            if (x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft <= p1MaxSize)
                                            {
                                                addCellLengthLeft.Add(x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft);
                                                addCellTotalLengthLeft = addCellTotalLengthLeft + x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft;

                                            }
                                            else
                                            {
                                                double len = x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft;
                                                double cellLength;
                                                for (int num = 1; true; num++)
                                                {
                                                    #region 寻找合适的单元格分配

                                                    cellLength = (double)((decimal)len / num);

                                                    if (cellLength <= p1MaxSize)
                                                    {

                                                        for (int n = 0; n < num; n++)
                                                        {
                                                            addCellLengthLeft.Add(cellLength);
                                                            addCellTotalLengthLeft = addCellTotalLengthLeft + cellLength;
                                                        }

                                                        break;


                                                    }
                                                    else
                                                    {
                                                        continue;
                                                    }

                                                    #endregion

                                                }


                                            }

                                            break;

                                            #region 下面的算法已经被上面几句简化方法代替掉

                                            //    //剩余距离平均分配

                                            //    //从已经添加的cell集合中取出最后一个添加的cell的长度，加入剩余长度中进行分配

                                            //    if (addCellLengthLeft.Count > 0)
                                            //    {
                                            //        addCellTotalLengthLeft = addCellTotalLengthLeft - addCellLengthLeft[addCellLengthLeft.Count - 1];
                                            //        addCellLengthLeft.RemoveAt(addCellLengthLeft.Count - 1);
                                            //    }


                                            //    double len = x1ToLeft - p1BaseSize * 0.5 - addCellTotalLengthLeft;

                                            //    #region

                                            //    if (len <= p1MaxSize)
                                            //    {
                                            //        //下面的方式不合理

                                            //        //分成一个即可  
                                            //        //if (addCellLengthLeft.Count > 0)
                                            //        //{
                                            //        //    if (len > addCellLengthLeft[addCellLengthLeft.Count - 1])
                                            //        //    {
                                            //        //        addCellLengthLeft.Add(len);
                                            //        //        addCellTotalLengthLeft = addCellTotalLengthLeft + len;
                                            //        //    }
                                            //        //    else
                                            //        //    {                                        
                                            //        //    }
                                            //        //}
                                            //        //else
                                            //        //{    
                                            //        //}
                                            //        addCellLengthLeft.Add(len);
                                            //        addCellTotalLengthLeft = addCellTotalLengthLeft + len;

                                            //    }
                                            //    else
                                            //    {
                                            //        double cellLength;

                                            //        #region

                                            //        for (int num = 1; true; num++)
                                            //        {
                                            //            #region 寻找合适的单元格分配

                                            //            cellLength = (double)((decimal)len / num);

                                            //            if (cellLength <= p1MaxSize)
                                            //            {
                                            //                if (addCellLengthLeft.Count > 0)
                                            //                {

                                            //                    if (cellLength >= addCellLengthLeft[addCellLengthLeft.Count - 1])
                                            //                    {
                                            //                        for (int n = 0; n < num; n++)
                                            //                        {
                                            //                            addCellLengthLeft.Add(cellLength);
                                            //                            addCellTotalLengthLeft = addCellTotalLengthLeft + cellLength;
                                            //                        }
                                            //                        break;

                                            //                    }
                                            //                    else
                                            //                    {
                                            //                        if (num == 1)
                                            //                        {
                                            //                            addCellLengthLeft.Add(cellLength);
                                            //                            addCellTotalLengthLeft = addCellTotalLengthLeft + cellLength;

                                            //                            break;

                                            //                        }
                                            //                        else if (num > 1)
                                            //                        {

                                            //                            cellLength = (double)((decimal)len / (num - 1));//即使结果 > MaxSize 也这样做

                                            //                            for (int n = 0; n < num - 1; n++)
                                            //                            {
                                            //                                addCellLengthLeft.Add(cellLength);
                                            //                                addCellTotalLengthLeft = addCellTotalLengthLeft + cellLength;
                                            //                            }

                                            //                            break;

                                            //                        }
                                            //                    }
                                            //                }
                                            //                else
                                            //                {
                                            //                    for (int n = 0; n < num; n++)
                                            //                    {
                                            //                        addCellLengthLeft.Add(cellLength);
                                            //                        addCellTotalLengthLeft = addCellTotalLengthLeft + cellLength;

                                            //                    }

                                            //                }
                                            //            }
                                            //            else
                                            //            {
                                            //                continue;
                                            //            }

                                            //            #endregion

                                            //        }//   for (int num = 1; true; num++)结束


                                            //    }

                                            //   #endregion

                                            //    break;



                                            //}

                                            //    #endregion

                                            #endregion


                                        }

                                    }
                                }// for (int i = 1; true; i++) 结束

                            }//       if (x1ToLeft > p1BaseSize * 0.5)结束






                            int xCoorIndex = XCoord.Count - 1;
                            if (addCellLengthLeft.Count > 0)
                            {

                                for (int i = addCellLengthLeft.Count - 1; i > -1; i--)
                                {
                                    XCoord.Add(XCoord[xCoorIndex] + addCellLengthLeft[i]);

                                    xCoorIndex = xCoorIndex + 1;

                                }
                            }

                            #endregion

                        }


                        //再往点的右侧
                        List<double> addCellLengthRight = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengthRight = 0;//记录上述List中已经添加的cell的长度的总和

                        if (Math.Abs(p1BaseSize - p1MaxSize) < 1e-6)// 认为 p1BaseSize =p1MaxSize 
                        {
                            //平分为2个

                            if (x1ToRight <= p1BaseSize * 0.5)
                            {
                                //左侧不添加
                            }
                            else
                            {
                                int cellNum = 2;
                                double ableLength = (x1ToRight - p1BaseSize * 0.5) / (double)cellNum;

                                for (int i = 0; i < cellNum; i++)
                                {
                                    addCellLengthRight.Add(ableLength);
                                    addCellTotalLengthRight = addCellTotalLengthRight + ableLength;
                                }

                            }



                            if (addCellLengthRight.Count > 0)
                            {
                                //首先添加点所在的单元格的右侧坐标

                                XCoord.Add(refinePoint1XYZ[0] + p1BaseSize * 0.5);

                                int cellIndex = XCoord.Count - 1;
                                for (int i = 0; i < addCellLengthRight.Count; i++)
                                {
                                    XCoord.Add(XCoord[cellIndex] + addCellLengthRight[i]);
                                    cellIndex = cellIndex + 1;
                                }
                            }
                            else
                            {
                                XCoord.Add(lowright[0]);
                            }




                        }
                        else if (p1BaseSize < p1MaxSize)
                        {
                            #region



                            if (x1ToRight <= p1BaseSize * 0.5)
                            {

                            }
                            else
                            {
                                for (int i = 1; true; i++)
                                {

                                    double cellLengthI;//合理的单元格的长度
                                    cellLengthI = Math.Pow(p1Bisa, i) * p1BaseSize;

                                    if (cellLengthI <= p1MaxSize) // cell的 Max 允许添加
                                    {
                                        //判断剩余距离是否够添加该长度Cell

                                        if (x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight >= cellLengthI)//距离足够
                                        {
                                            addCellLengthRight.Add(cellLengthI);
                                            addCellTotalLengthRight = addCellTotalLengthRight + cellLengthI;


                                        }
                                        else
                                        {
                                            //距离不够添加该cell

                                            addCellLengthRight.Add(x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight);
                                            addCellTotalLengthRight = addCellTotalLengthRight + x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight;


                                            #region 下面的算法已经被上面几句简化方法代替


                                            //if (addCellLengthRight.Count > 0)
                                            //{
                                            //    #region

                                            //    if (x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight >= addCellLengthRight[addCellLengthRight.Count - 1])
                                            //    {

                                            //        addCellLengthRight.Add(x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight);
                                            //        addCellTotalLengthRight = addCellTotalLengthRight + x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight;


                                            //    }
                                            //    else
                                            //    {

                                            //        addCellTotalLengthRight = addCellTotalLengthRight - addCellLengthRight[addCellLengthRight.Count - 1];
                                            //        addCellLengthRight.RemoveAt(addCellLengthRight.Count - 1);

                                            //        //剩余距离分配

                                            //        if (x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight > p1MaxSize)
                                            //        {

                                            //            double lenTotal = x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight;

                                            //            for (int num = 1; true; num++)
                                            //            {
                                            //                //计算分割为几个单元格比较合适

                                            //                double everageCellLen = (double)((decimal)lenTotal / num);

                                            //                if (everageCellLen > p1MaxSize)
                                            //                {
                                            //                    double everage = (double)((decimal)lenTotal / (num + 1));
                                            //                    if (everage <= addCellLengthRight[addCellLengthRight.Count - 1])
                                            //                    {
                                            //                        //添加上吧
                                            //                        for (int index = 0; index < num; index++)
                                            //                        {
                                            //                            addCellLengthRight.Add(everageCellLen);
                                            //                            addCellTotalLengthRight = addCellTotalLengthRight + everageCellLen;
                                            //                        }

                                            //                        break;

                                            //                    }
                                            //                    else
                                            //                    {
                                            //                        //继续找
                                            //                        continue;
                                            //                    }

                                            //                }
                                            //                else
                                            //                {
                                            //                    //就这样分配吧

                                            //                    for (int index = 0; index < num; index++)
                                            //                    {
                                            //                        addCellLengthRight.Add(everageCellLen);
                                            //                        addCellTotalLengthRight = addCellTotalLengthRight + everageCellLen;
                                            //                    }

                                            //                    break;
                                            //                }


                                            //            }// for (int num = 2; true ; num++)  结束


                                            //        }
                                            //        else
                                            //        {
                                            //            //剩余的分配为1个

                                            //            addCellLengthRight.Add(x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight);
                                            //            addCellTotalLengthRight = addCellTotalLengthRight + x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight;

                                            //        }

                                            //        //break;
                                            //    }

                                            //    break;

                                            //    #endregion


                                            //}
                                            //else
                                            //{
                                            //    addCellLengthRight.Add(x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight);
                                            //    addCellTotalLengthRight = addCellTotalLengthRight + x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight;

                                            //}

                                            #endregion


                                            break;


                                        }


                                    }
                                    else
                                    {
                                        //例如 5,2,20  达到了25   MaxSize不允许



                                        //如果正好能够平均分配整数个MaxSize的单元格， 

                                        double ableLen = (x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight);//剩余距离



                                        int cellNum20 = (int)Math.Ceiling(ableLen % p1MaxSize);//能够按照每个MaxSize正好分配  防止0.25等的情况


                                        if (ableLen >= p1MaxSize && cellNum20 == 0)
                                        {
                                            //平均分配

                                            int number = (int)(ableLen / p1MaxSize);
                                            for (int index = 0; index < number; index++)
                                            {
                                                addCellLengthRight.Add(p1MaxSize);
                                                addCellTotalLengthRight = addCellTotalLengthRight + p1MaxSize;
                                            }

                                            break;
                                        }
                                        else
                                        {

                                            //剩余距离分配

                                            if (x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight <= p1MaxSize)
                                            {
                                                addCellLengthRight.Add(x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight);
                                                addCellTotalLengthRight = addCellTotalLengthRight + x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight;

                                            }
                                            else
                                            {
                                                double len = x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight;
                                                double cellLength;
                                                for (int num = 1; true; num++)
                                                {
                                                    #region 寻找合适的单元格分配

                                                    cellLength = (double)((decimal)len / num);

                                                    if (cellLength <= p1MaxSize)
                                                    {

                                                        for (int n = 0; n < num; n++)
                                                        {
                                                            addCellLengthRight.Add(cellLength);
                                                            addCellTotalLengthRight = addCellTotalLengthRight + cellLength;
                                                        }

                                                        break;


                                                    }
                                                    else
                                                    {
                                                        continue;
                                                    }

                                                    #endregion

                                                }


                                            }


                                            #region 下面的方法已经被上面几句简化方法替换掉


                                            ////从已经添加的cell集合中取出最后一个添加的cell的长度，加入剩余长度中进行分配

                                            //if (addCellLengthRight.Count > 0)
                                            //{
                                            //    addCellTotalLengthRight = addCellTotalLengthRight - addCellLengthRight[addCellLengthRight.Count - 1];
                                            //    addCellLengthRight.RemoveAt(addCellLengthRight.Count - 1);
                                            //}

                                            //double len = x1ToRight - p1BaseSize * 0.5 - addCellTotalLengthRight;//剩余距离

                                            //if (len <= p1MaxSize)
                                            //{
                                            //    //分成一个即可
                                            //    addCellLengthRight.Add(len);
                                            //    addCellTotalLengthRight = addCellTotalLengthRight + len;

                                            //}
                                            //else
                                            //{
                                            //    //if (len > p1MaxSize)

                                            //    double cellLength;

                                            //    #region

                                            //    for (int num = 1; true; num++)
                                            //    {
                                            //        #region 寻找合适的单元格分配  逐渐增加单元格的个数

                                            //        cellLength = (double)((decimal)len / num);

                                            //        if (cellLength <= p1MaxSize)
                                            //        {
                                            //            if (cellLength >= addCellLengthRight[addCellLengthRight.Count - 1])
                                            //            {
                                            //                addCellLengthRight.Add(cellLength);
                                            //                addCellTotalLengthRight = addCellTotalLengthRight + cellLength;

                                            //                break;

                                            //            }
                                            //            else
                                            //            {
                                            //                //下面的处理有点不合理  暂时这样吧

                                            //                #region

                                            //                if (num == 1)
                                            //                {
                                            //                    addCellLengthRight.Add(cellLength);
                                            //                    addCellTotalLengthRight = addCellTotalLengthRight + cellLength;

                                            //                    break;

                                            //                }
                                            //                else if (num > 1)
                                            //                {

                                            //                    cellLength = (double)((decimal)len / (num - 1));//即使结果 > MaxSize 也这样做

                                            //                    addCellLengthRight.Add(cellLength);
                                            //                    addCellTotalLengthRight = addCellTotalLengthRight + cellLength;

                                            //                    break;

                                            //                }

                                            //                #endregion

                                            //            }
                                            //        }
                                            //        else
                                            //        {
                                            //            continue;
                                            //        }

                                            //        #endregion

                                            //    }//   for (int num = 1; true; num++)结束

                                            //    #endregion

                                            //}
                                            #endregion

                                            break;


                                        }



                                    }

                                }// for (int i = 1; true; i++) 结束

                            }//       if (x1ToRight > p1BaseSize * 0.5)结束









                            #region  添加点的右侧X方向坐标


                            if (addCellLengthRight.Count > 0)
                            {
                                //首先添加点所在的单元格的右侧坐标

                                XCoord.Add(refinePoint1XYZ[0] + p1BaseSize * 0.5);

                                int cellIndex = XCoord.Count - 1;
                                for (int i = 0; i < addCellLengthRight.Count; i++)
                                {
                                    XCoord.Add(XCoord[cellIndex] + addCellLengthRight[i]);
                                    cellIndex = cellIndex + 1;
                                }
                            }
                            else
                            {
                                XCoord.Add(lowright[0]);
                            }


                            #endregion


                            #endregion

                        }




                    }//Bias>1


                }
                //else
                //{  //Bias<0

                //}

                #endregion


                return XCoord;

            }
            catch
            {
                XCoord = new List<double>();

                return XCoord;
            }

        }

        /// <summary>
        /// 只有一个RefinePoint点的情况  返回 YCoord   出错时  List<double> YCoord .Count=0
        /// </summary>
        public static List<double> CreateGridOneRefinePointsYCoor(GridFrameParameter gridFrame, RefinePoint refinePoint)
        {

            //说明：首先找到最后一个合理的单元格 限制条件：长度小于MaxSize；已使用距离足够（单元格长度加和）
            //若剩余距离够整数个MaxSize，恰好分；若不是整数个，把最后一个加进来，平均分距离

            List<double> YCoord = new List<double>();




            if (gridFrame == null || refinePoint == null)
            {
                return YCoord;
            }

            try
            {

                #region


                #region 首先定义一些必须的数据信息

                //网格边框信息
                double rotate = gridFrame.RotateZ;
                double XWidth = gridFrame.XWidth;
                double YHeight = gridFrame.YHeight;
                double[] lowleft = new double[3];
                lowleft[0] = gridFrame.LowLeft.XCoordinate;
                lowleft[1] = gridFrame.LowLeft.YCoordinate;
                lowleft[2] = 0;
                double[] upleft = new double[3];
                upleft[0] = gridFrame.UpLeft.XCoordinate;
                upleft[1] = gridFrame.UpLeft.YCoordinate;
                upleft[2] = 0;

                double[] upright = new double[3];
                upright[0] = gridFrame.UpRight.XCoordinate;
                upright[1] = gridFrame.UpRight.YCoordinate;
                upright[2] = 0;
                double[] lowright = new double[3];
                lowright[0] = gridFrame.LowRight.XCoordinate;
                lowright[1] = gridFrame.LowRight.YCoordinate;
                lowright[2] = 0;



                //点1信息及和网格边框的关系
                int refinePointNum = 1;

                double[] refinePoint1XYZ = new double[3];
                refinePoint1XYZ[0] = refinePoint.RefinePointCoor.XCoordinate;
                refinePoint1XYZ[1] = refinePoint.RefinePointCoor.YCoordinate;
                refinePoint1XYZ[2] = 0;

                double p1BaseSize = refinePoint.BaseSize;
                double p1Bisa = refinePoint.Bisa;
                double p1MaxSize = refinePoint.MaxSize;



                double y1ToTop =(upleft[1] - refinePoint1XYZ[1]);
                double y1ToBottom =  (refinePoint1XYZ[1] - lowleft[1]);


                #endregion

                #region 部分参数的有效性检验

                if (p1BaseSize <= 0 || p1MaxSize <= 0)
                {
                    return YCoord;
                }
                if (p1MaxSize < p1BaseSize)
                {
                    return YCoord;
                }

                //该参数先按照简化情况处理

                if (p1Bisa < 0)
                {
                    return YCoord;
                }

                #endregion




                YCoord.Add(lowleft [1]);//左上角第一个点的Y坐标



                //if (p1Bisa > 0)
                {
                    if (p1Bisa < 1)
                    {

                        //暂时按照平分为2个

                        List<double> addCellLengthBottom = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengthBottom = 0;//记录上述List中已经添加的cell的长度的总和


                        #region 下侧


                        //平分为 2 个

                        if (y1ToBottom <= p1BaseSize * 0.5)
                        {
                            //左侧不添加
                        }
                        else
                        {
                            int cellNum = 2;
                            double ableLength = (y1ToBottom - p1BaseSize * 0.5) / (double)cellNum;

                            for (int i = 0; i < cellNum; i++)
                            {
                                addCellLengthBottom.Add(ableLength);
                                addCellTotalLengthBottom = addCellTotalLengthBottom + ableLength;
                            }
                        }


                        if (addCellLengthBottom.Count > 0)
                        {

                            int cellIndex = YCoord.Count - 1;
                            for (int i = addCellLengthBottom.Count - 1; i > -1; i--)
                            {
                                YCoord.Add(YCoord[cellIndex] + addCellLengthBottom[i]);
                                cellIndex = cellIndex + 1;
                            }
                        }

                        #endregion

                        List<double> addCellLengthTop = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengtTop = 0;//记录上述List中已经添加的cell的长度的总和


                        #region 上侧


                        //平分为 2 个

                        if (y1ToTop <= p1BaseSize * 0.5)
                        {
                            //左侧不添加
                        }
                        else
                        {
                            int cellNum = 2;
                            double ableLength = (y1ToTop - p1BaseSize * 0.5) / (double)cellNum;

                            for (int i = 0; i < cellNum; i++)
                            {
                                addCellLengthTop.Add(ableLength);
                                addCellTotalLengtTop = addCellTotalLengtTop + ableLength;
                            }
                        }



                        if (addCellLengthTop.Count > 0)
                        {
                            int yCoorIndex = YCoord.Count - 1;

                            YCoord.Add(refinePoint1XYZ[1] + p1BaseSize * 0.5);

                            yCoorIndex = YCoord.Count - 1;

                            for (int i = 0; i < addCellLengthTop.Count; i++)
                            {
                                YCoord.Add(YCoord[yCoorIndex] + addCellLengthTop[i]);

                                yCoorIndex = yCoorIndex + 1;

                            }
                        }
                        else
                        {
                            YCoord.Add(upleft[1]);
                        }

                        #endregion

                    }
                    else if (p1Bisa == 1)
                    {

                        #region//往点的下侧走

                        List<double> addCellLengthBottom = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengthBottom = 0;//记录上述List中已经添加的cell的长度的总和

                        if (y1ToBottom <= p1BaseSize * 0.5)
                        {
                            //左侧不添加
                        }
                        else
                        {

                            double ableLength = y1ToBottom - p1BaseSize * 0.5;

                            int cellNum = (int)Math.Ceiling(ableLength / p1BaseSize);

                            double cellLength = ableLength / (double)cellNum;


                            for (int i = 0; i < cellNum; i++)
                            {
                                addCellLengthBottom.Add(cellLength);
                                addCellTotalLengthBottom = addCellTotalLengthBottom + cellLength;
                            }

                            //for (int i = 1; true; i++)
                            //{
                            //    double averageCellLen = ableLength / i;

                            //    if (averageCellLen <= p1MaxSize)
                            //    {
                            //        for (int index = 0; index < i; index++)
                            //        {
                            //            addCellLengthBottom.Add(averageCellLen);
                            //            addCellTotalLengthBottom = addCellTotalLengthBottom + averageCellLen;
                            //        }
                            //        break;
                            //    }
                            //    else
                            //    {
                            //        continue;
                            //    }

                            //}



                        }


                        #region 添加点的下侧Y方向坐标


                        if (addCellLengthBottom.Count > 0)
                        {

                            int cellIndex = YCoord.Count - 1;
                            for (int i = addCellLengthBottom.Count - 1; i > -1; i--)
                            {
                                YCoord.Add(YCoord[cellIndex] + addCellLengthBottom[i]);
                                cellIndex = cellIndex + 1;
                            }
                        }

                        #endregion

                        #endregion



                        #region //往点的上侧走

                        List<double> addCellLengthTop = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengtTop = 0;//记录上述List中已经添加的cell的长度的总和

                        if (y1ToTop <= p1BaseSize * 0.5)
                        {
                            //上侧不添加
                        }
                        else
                        {
                            double ableLength = y1ToTop - p1BaseSize * 0.5;

                            int cellNum = (int)Math.Ceiling(ableLength / p1BaseSize);

                            double cellLength = ableLength / (double)cellNum;


                            for (int i = 0; i < cellNum; i++)
                            {
                                addCellLengthTop.Add(cellLength);
                                addCellTotalLengtTop = addCellTotalLengtTop + cellLength;
                            }

                            //for (int i = 1; true; i++)
                            //{
                            //    double averageCellLen = ableLength / i;

                            //    if (averageCellLen <= p1MaxSize)
                            //    {
                            //        for (int index = 0; index < i; index++)
                            //        {
                            //            addCellLengthTop.Add(averageCellLen);
                            //            addCellTotalLengtTop = addCellTotalLengtTop + averageCellLen;
                            //        }
                            //        break;
                            //    }
                            //    else
                            //    {
                            //        continue;
                            //    }
                            //}


                        }

                        #region 添加点的上侧Y方向坐标


                        if (addCellLengthTop.Count > 0)
                        {
                            int yCoorIndex = YCoord.Count - 1;

                            YCoord.Add(refinePoint1XYZ[1] + p1BaseSize * 0.5);
                            yCoorIndex = YCoord.Count - 1;

                            for (int i = 0; i < addCellLengthTop.Count; i++)
                            {
                                if (yCoorIndex >= 0)
                                {
                                    YCoord.Add(YCoord[yCoorIndex] + addCellLengthTop[i]);

                                    yCoorIndex = yCoorIndex + 1;
                                }
                                else
                                {
                                    YCoord.Add(addCellLengthTop[i]);
                                    yCoorIndex = yCoorIndex + 1;
                                }
                            }
                        }
                        else
                        {
                            YCoord.Add(upleft[1]);
                        }

                        #endregion

                        #endregion


                    }
                    else if (p1Bisa > 1)
                    {

                        // 下面是具体的计算网格分布的方法


                        //往点的下侧走


                        #region  //往点的下侧走


                        List<double> addCellLengthBottom = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengthBottom = 0;//记录上述List中已经添加的cell的长度的总和

                        if (Math.Abs(p1BaseSize - p1MaxSize) < 1e-6)// 认为 p1BaseSize =p1MaxSize
                        {
                            //平分为 2 个

                            if (y1ToBottom <= p1BaseSize * 0.5)
                            {
                                //左侧不添加
                            }
                            else
                            {
                                int cellNum = 2;
                                double ableLength = (y1ToBottom - p1BaseSize * 0.5) / (double)cellNum;

                                for (int i = 0; i < cellNum; i++)
                                {
                                    addCellLengthBottom.Add(ableLength);
                                    addCellTotalLengthBottom = addCellTotalLengthBottom + ableLength;
                                }
                            }


                            if (addCellLengthBottom.Count > 0)
                            {

                                int cellIndex = YCoord.Count - 1;
                                for (int i = addCellLengthBottom.Count - 1; i > -1; i--)
                                {
                                    YCoord.Add(YCoord[cellIndex] + addCellLengthBottom[i]);
                                    cellIndex = cellIndex + 1;
                                }
                            }



                        }
                        else if (p1BaseSize < p1MaxSize)
                        {
                            #region


                            if (y1ToBottom <= p1BaseSize * 0.5)
                            {

                            }
                            else
                            {
                                for (int i = 1; true; i++)
                                {

                                    double cellLengthI;//合理的单元格的长度
                                    cellLengthI = Math.Pow(p1Bisa, i) * p1BaseSize;

                                    if (cellLengthI <= p1MaxSize) // cell的 Max 允许添加
                                    {
                                        //判断剩余距离是否够添加该长度Cell

                                        #region

                                        if (y1ToBottom - p1BaseSize * 0.5 - addCellTotalLengthBottom >= cellLengthI)//距离足够
                                        {
                                            addCellLengthBottom.Add(cellLengthI);
                                            addCellTotalLengthBottom = addCellTotalLengthBottom + cellLengthI;


                                        }
                                        else
                                        {
                                            //距离不够添加该cell

                                            addCellLengthBottom.Add(y1ToBottom - p1BaseSize * 0.5 - addCellTotalLengthBottom);
                                            addCellTotalLengthBottom = addCellTotalLengthBottom + y1ToBottom - p1BaseSize * 0.5 - addCellTotalLengthBottom;

                                            #region 未使用

                                            //if (addCellLengthBottom.Count > 0)
                                            //{
                                            //    #region

                                            //    if (y1ToTop - p1BaseSize * 0.5 - addCellTotalLengthBottom >= addCellLengthBottom[addCellLengthBottom.Count - 1])
                                            //    {

                                            //        addCellLengthBottom.Add(y1ToTop - p1BaseSize * 0.5 - addCellTotalLengthBottom);
                                            //        addCellTotalLengthBottom = addCellTotalLengthBottom + y1ToTop - p1BaseSize * 0.5 - addCellTotalLengthBottom;


                                            //    }
                                            //    else
                                            //    {

                                            //        addCellTotalLengthBottom = addCellTotalLengthBottom - addCellLengthBottom[addCellLengthBottom.Count - 1];
                                            //        addCellLengthBottom.RemoveAt(addCellLengthBottom.Count - 1);

                                            //        //剩余距离分配

                                            //        if (y1ToTop - p1BaseSize * 0.5 - addCellTotalLengthBottom > p1MaxSize)
                                            //        {

                                            //            double lenTotal = y1ToTop - p1BaseSize * 0.5 - addCellTotalLengthBottom;

                                            //            for (int num = 1; true; num++)
                                            //            {
                                            //                //计算分割为几个单元格比较合适

                                            //                double everageCellLen = (double)((decimal)lenTotal / num);

                                            //                if (everageCellLen > p1MaxSize)
                                            //                {
                                            //                    double everage = (double)((decimal)lenTotal / (num + 1));
                                            //                    if (everage <= addCellLengthBottom[addCellLengthBottom.Count - 1])
                                            //                    {
                                            //                        //添加上吧
                                            //                        for (int index = 0; index < num; index++)
                                            //                        {
                                            //                            addCellLengthBottom.Add(everageCellLen);
                                            //                            addCellTotalLengthBottom = addCellTotalLengthBottom + everageCellLen;
                                            //                        }

                                            //                        break;

                                            //                    }
                                            //                    else
                                            //                    {
                                            //                        //继续找
                                            //                        continue;
                                            //                    }

                                            //                }
                                            //                else
                                            //                {
                                            //                    //就这样分配吧

                                            //                    for (int index = 0; index < num; index++)
                                            //                    {
                                            //                        addCellLengthBottom.Add(everageCellLen);
                                            //                        addCellTotalLengthBottom = addCellTotalLengthBottom + everageCellLen;
                                            //                    }

                                            //                    break;
                                            //                }


                                            //            }// for (int num = 2; true ; num++)  结束


                                            //        }
                                            //        else
                                            //        {
                                            //            //剩余的分配为1个

                                            //            addCellLengthBottom.Add(y1ToTop - p1BaseSize * 0.5 - addCellTotalLengthBottom);
                                            //            addCellTotalLengthBottom = addCellTotalLengthBottom + y1ToTop - p1BaseSize * 0.5 - addCellTotalLengthBottom;

                                            //        }

                                            //        //break;
                                            //    }

                                            //    break;

                                            //    #endregion


                                            //}
                                            //else
                                            //{
                                            //    addCellLengthBottom.Add(y1ToTop - p1BaseSize * 0.5 - addCellTotalLengthBottom);
                                            //    addCellTotalLengthBottom = addCellTotalLengthBottom + y1ToTop - p1BaseSize * 0.5 - addCellTotalLengthBottom;

                                            //}


                                            #endregion

                                            break;


                                        }
                                        #endregion

                                    }
                                    else
                                    {
                                        //例如 5,2,20  达到了25   MaxSize不允许

                                        #region

                                        //如果正好能够平均分配整数个MaxSize的单元格， 

                                        double ableLen = (y1ToBottom - p1BaseSize * 0.5 - addCellTotalLengthBottom);//剩余距离

                                        int cellNum20 = (int)Math.Ceiling(ableLen % p1MaxSize);//能够按照每个MaxSize正好分配  防止0.25等的情况

                                        if (ableLen >= p1MaxSize && cellNum20 == 0)
                                        {
                                            //平均分配

                                            int number = (int)(ableLen / p1MaxSize);
                                            for (int index = 0; index < number; index++)
                                            {
                                                addCellLengthBottom.Add(p1MaxSize);
                                                addCellTotalLengthBottom = addCellTotalLengthBottom + p1MaxSize;
                                            }

                                            break;
                                        }
                                        else
                                        {

                                            //剩余距离分配
                                            //从已经添加的cell集合中取出最后一个添加的cell的长度，加入剩余长度中进行分配


                                            double len = y1ToBottom - p1BaseSize * 0.5 - addCellTotalLengthBottom;

                                            if (len <= p1MaxSize)
                                            {
                                                //分成一个即可
                                                addCellLengthBottom.Add(len);
                                                addCellTotalLengthBottom = addCellTotalLengthBottom + len;
                                            }
                                            else
                                            {
                                                double cellLength;

                                                for (int num = 1; true; num++)
                                                {
                                                    #region 寻找合适的单元格分配

                                                    cellLength = (double)((decimal)len / num);

                                                    if (cellLength <= p1MaxSize)
                                                    {

                                                        for (int n = 0; n < num; n++)
                                                        {
                                                            addCellLengthBottom.Add(cellLength);
                                                            addCellTotalLengthBottom = addCellTotalLengthBottom + cellLength;
                                                        }

                                                        break;


                                                    }
                                                    else
                                                    {
                                                        continue;
                                                    }
                                                    #endregion

                                                }
                                            }


                                            #region


                                            //if (addCellLengthBottom.Count > 0)
                                            //{
                                            //    addCellTotalLengthBottom = addCellTotalLengthBottom - addCellLengthBottom[addCellLengthBottom.Count - 1];
                                            //    addCellLengthBottom.RemoveAt(addCellLengthBottom.Count - 1);
                                            //}

                                            //double len = y1ToTop - p1BaseSize * 0.5 - addCellTotalLengthBottom;//剩余距离

                                            //if (len <= p1MaxSize)
                                            //{
                                            //    //分成一个即可
                                            //    addCellLengthBottom.Add(len);
                                            //    addCellTotalLengthBottom = addCellTotalLengthBottom + len;

                                            //}
                                            //else
                                            //{
                                            //    //if (len > p1MaxSize)

                                            //    double cellLength;

                                            //    #region

                                            //    for (int num = 1; true; num++)
                                            //    {
                                            //        #region 寻找合适的单元格分配  逐渐增加单元格的个数

                                            //        cellLength = (double)((decimal)len / num);

                                            //        if (cellLength <= p1MaxSize)
                                            //        {
                                            //            if (cellLength >= addCellLengthBottom[addCellLengthBottom.Count - 1])
                                            //            {
                                            //                addCellLengthBottom.Add(cellLength);
                                            //                addCellTotalLengthBottom = addCellTotalLengthBottom + cellLength;

                                            //                break;

                                            //            }
                                            //            else
                                            //            {
                                            //                //下面的处理有点不合理  暂时这样吧

                                            //                #region

                                            //                if (num == 1)
                                            //                {
                                            //                    addCellLengthBottom.Add(cellLength);
                                            //                    addCellTotalLengthBottom = addCellTotalLengthBottom + cellLength;

                                            //                    break;

                                            //                }
                                            //                else if (num > 1)
                                            //                {

                                            //                    cellLength = (double)((decimal)len / (num - 1));//即使结果 > MaxSize 也这样做

                                            //                    addCellLengthBottom.Add(cellLength);
                                            //                    addCellTotalLengthBottom = addCellTotalLengthBottom + cellLength;

                                            //                    break;

                                            //                }

                                            //                #endregion

                                            //            }
                                            //        }
                                            //        else
                                            //        {
                                            //            continue;
                                            //        }

                                            //        #endregion

                                            //    }//   for (int num = 1; true; num++)结束

                                            //    #endregion

                                            //}
                                            #endregion

                                            break;


                                        }

                                        #endregion

                                    }

                                }// for (int i = 1; true; i++) 结束

                            }//       if (y1ToTop > p1BaseSize * 0.5)结束






                            #endregion

                            #region  添加点的下侧Y方向坐标


                            if (addCellLengthBottom.Count > 0)
                            {

                                int cellIndex = YCoord.Count - 1;
                                for (int i = addCellLengthBottom.Count - 1; i > -1; i--)
                                {
                                    YCoord.Add(YCoord[cellIndex] + addCellLengthBottom[i]);
                                    cellIndex = cellIndex + 1;
                                }
                            }



                            #endregion

                        }





                        #endregion


                        #region //往点的上侧走

                        List<double> addCellLengthTop = new List<double>();//记录添加的合理单元格的长度
                        double addCellTotalLengtTop = 0;//记录上述List中已经添加的cell的长度的总和

                        if (Math.Abs(p1BaseSize - p1MaxSize) < 1e-6)// 认为 p1BaseSize =p1MaxSize
                        {
                            //平分为 2 个

                            if (y1ToTop <= p1BaseSize * 0.5)
                            {
                                //左侧不添加
                            }
                            else
                            {
                                int cellNum = 2;
                                double ableLength = (y1ToTop - p1BaseSize * 0.5) / (double)cellNum;

                                for (int i = 0; i < cellNum; i++)
                                {
                                    addCellLengthTop.Add(ableLength);
                                    addCellTotalLengtTop = addCellTotalLengtTop + ableLength;
                                }
                            }



                            if (addCellLengthTop.Count > 0)
                            {
                                int yCoorIndex = YCoord.Count - 1;

                                YCoord.Add(refinePoint1XYZ[1] + p1BaseSize * 0.5);

                                yCoorIndex = YCoord.Count - 1;

                                for (int i = 0; i < addCellLengthTop.Count; i++)
                                {
                                    YCoord.Add(YCoord[yCoorIndex] + addCellLengthTop[i]);

                                    yCoorIndex = yCoorIndex + 1;

                                }
                            }
                            else
                            {
                                YCoord.Add(upleft[1]);
                            }


                        }
                        else if (p1BaseSize < p1MaxSize)
                        {


                            #region

                            if (y1ToTop <= p1BaseSize * 0.5)
                            {
                                //左侧不添加
                            }
                            else
                            {
                                for (int i = 1; true; i++)
                                {

                                    double cellLengthI;//合理的单元格的长度
                                    cellLengthI = Math.Pow(p1Bisa, i) * p1BaseSize;

                                    if (cellLengthI <= p1MaxSize)//cell的Max允许添加
                                    {
                                        //判断剩余距离是否够添加该长度Cell

                                        #region

                                        if (y1ToTop - p1BaseSize * 0.5 - addCellTotalLengtTop >= cellLengthI)//距离足够
                                        {
                                            addCellLengthTop.Add(cellLengthI);
                                            addCellTotalLengtTop = addCellTotalLengtTop + cellLengthI;


                                        }
                                        else
                                        {
                                            //距离不够添加该cell

                                            addCellLengthTop.Add(y1ToTop - p1BaseSize * 0.5 - addCellTotalLengtTop);
                                            addCellTotalLengtTop = addCellTotalLengtTop + y1ToTop - p1BaseSize * 0.5 - addCellTotalLengtTop;


                                            #region 下面的方法已经被上面几句简化方法代替

                                            //if (addCellLengthTop .Count > 0)
                                            //{
                                            //    #region

                                            //    if (y1ToTop  - p1BaseSize * 0.5 - addCellTotalLengtTop  >= addCellLengthTop [addCellLengthTop .Count - 1])
                                            //    {

                                            //        addCellLengthTop .Add(y1ToTop  - p1BaseSize * 0.5 - addCellTotalLengtTop );
                                            //        addCellTotalLengtTop  = addCellTotalLengtTop  + y1ToTop  - p1BaseSize * 0.5 - addCellTotalLengtTop ;


                                            //    }
                                            //    else
                                            //    {

                                            //        addCellTotalLengtTop  = addCellTotalLengtTop  - addCellLengthTop [addCellLengthTop .Count - 1];
                                            //        addCellLengthTop .RemoveAt(addCellLengthTop .Count - 1);

                                            //        //剩余距离分配

                                            //        if (y1ToTop  - p1BaseSize * 0.5 - addCellTotalLengtTop  > p1MaxSize)
                                            //        {

                                            //            double lenTotal = y1ToTop  - p1BaseSize * 0.5 - addCellTotalLengtTop ;

                                            //            for (int num = 1; true; num++)
                                            //            {
                                            //                //计算分割为几个单元格比较合适

                                            //                double everageCellLen = (double)((decimal)lenTotal / num);

                                            //                if (everageCellLen > p1MaxSize)
                                            //                {
                                            //                    double everage = (double)((decimal)lenTotal / (num + 1));
                                            //                    if (everage <= addCellLengthTop [addCellLengthTop .Count - 1])
                                            //                    {
                                            //                        //添加上吧
                                            //                        for (int index = 0; index < num; index++)
                                            //                        {
                                            //                            addCellLengthTop .Add(everageCellLen);
                                            //                            addCellTotalLengtTop  = addCellTotalLengtTop  + everageCellLen;
                                            //                        }

                                            //                        break;

                                            //                    }
                                            //                    else
                                            //                    {
                                            //                        //继续找
                                            //                        continue;
                                            //                    }

                                            //                }
                                            //                else
                                            //                {
                                            //                    //就这样分配吧

                                            //                    for (int index = 0; index < num; index++)
                                            //                    {
                                            //                        addCellLengthTop.Add(everageCellLen);
                                            //                        addCellTotalLengtTop = addCellTotalLengtTop + everageCellLen;
                                            //                    }

                                            //                    break;
                                            //                }


                                            //            }// for (int num = 2; true ; num++)  结束


                                            //        }
                                            //        else
                                            //        {
                                            //            //剩余的分配为1个

                                            //            addCellLengthTop.Add(y1ToTop  - p1BaseSize * 0.5 - addCellTotalLengtTop);
                                            //            addCellTotalLengtTop = addCellTotalLengtTop + y1ToTop  - p1BaseSize * 0.5 - addCellTotalLengtTop;

                                            //        }

                                            //        //break;
                                            //    }

                                            //    break;

                                            //    #endregion


                                            //}
                                            //else
                                            //{

                                            //    //添加的第一个cell  距离不够
                                            //    if (y1ToTop - p1BaseSize * 0.5 - addCellTotalLengtTop < p1BaseSize)
                                            //    {
                                            //    }
                                            //    else
                                            //    {
                                            //        addCellLengthTop.Add(y1ToTop - p1BaseSize * 0.5 - addCellTotalLengtTop);
                                            //        addCellTotalLengtTop = addCellTotalLengtTop + y1ToTop - p1BaseSize * 0.5 - addCellTotalLengtTop;
                                            //    }

                                            //}

                                            #endregion

                                            break;


                                        }
                                        #endregion

                                    }
                                    else
                                    {
                                        //例如 5,2,20  达到了25   MaxSize不允许

                                        #region

                                        //如果正好能够平均分配整数个MaxSize的单元格， 

                                        double ableLen = (y1ToTop - p1BaseSize * 0.5 - addCellTotalLengtTop);//剩余距离

                                        int cellNum20 = (int)Math.Ceiling(ableLen % p1MaxSize);//能够按照每个MaxSize正好分配  防止0.25等的情况



                                        if (ableLen >= p1MaxSize && cellNum20 == 0)
                                        {
                                            //平均分配

                                            int number = (int)((y1ToTop - p1BaseSize * 0.5 - addCellTotalLengtTop) / p1MaxSize);
                                            for (int index = 0; index < number; index++)
                                            {
                                                addCellLengthTop.Add(p1MaxSize);
                                                addCellTotalLengtTop = addCellTotalLengtTop + p1MaxSize;
                                            }

                                            break;
                                        }
                                        else
                                        {

                                            //剩余距离平均分配

                                            double len = y1ToTop - p1BaseSize * 0.5 - addCellTotalLengtTop;

                                            if (len <= p1MaxSize)
                                            {
                                                //分成一个即可
                                                addCellLengthTop.Add(len);
                                                addCellTotalLengtTop = addCellTotalLengtTop + len;
                                            }
                                            else
                                            {
                                                double cellLength;

                                                for (int num = 1; true; num++)
                                                {
                                                    #region 寻找合适的单元格分配

                                                    cellLength = (double)((decimal)len / num);

                                                    if (cellLength <= p1MaxSize)
                                                    {

                                                        for (int n = 0; n < num; n++)
                                                        {
                                                            addCellLengthTop.Add(cellLength);
                                                            addCellTotalLengtTop = addCellTotalLengtTop + cellLength;
                                                        }

                                                        break;


                                                    }
                                                    else
                                                    {
                                                        continue;
                                                    }
                                                    #endregion


                                                }
                                            }

                                            #region 下面算法被简化方案替换


                                            ////从已经添加的cell集合中取出最后一个添加的cell的长度，加入剩余长度中进行分配

                                            //if (addCellLengthTop .Count > 0)
                                            //{
                                            //    addCellTotalLengtTop  = addCellTotalLengtTop  - addCellLengthTop [addCellLengthTop .Count - 1];
                                            //    addCellLengthTop .RemoveAt(addCellLengthTop .Count - 1);
                                            //}

                                            //double len = y1ToTop  - p1BaseSize * 0.5 - addCellTotalLengtTop ;

                                            //if (len <= p1MaxSize)
                                            //{
                                            //    //分成一个即可
                                            //    addCellLengthTop .Add(len);
                                            //    addCellTotalLengtTop  = addCellTotalLengtTop  + len;
                                            //}
                                            //else
                                            //{
                                            //    double cellLength;

                                            //    for (int num = 1; true; num++)
                                            //    {
                                            //        #region 寻找合适的单元格分配

                                            //        cellLength = (double)((decimal)len / num);

                                            //        if (cellLength <= p1MaxSize)
                                            //        {

                                            //            if (addCellLengthTop.Count > 0)
                                            //            {

                                            //                if (cellLength >= addCellLengthTop[addCellLengthTop.Count - 1])
                                            //                {
                                            //                    for (int n = 0; n < num; n++)
                                            //                    {

                                            //                        addCellLengthTop.Add(cellLength);
                                            //                        addCellTotalLengtTop = addCellTotalLengtTop + cellLength;
                                            //                    }
                                            //                    break;

                                            //                }
                                            //                else
                                            //                {
                                            //                    if (num == 1)
                                            //                    {
                                            //                        addCellLengthTop.Add(cellLength);
                                            //                        addCellTotalLengtTop = addCellTotalLengtTop + cellLength;

                                            //                        break;

                                            //                    }
                                            //                    else if (num > 1)
                                            //                    {

                                            //                        cellLength = (double)((decimal)len / (num - 1));//即使结果 > MaxSize 也这样做

                                            //                        for (int n = 0; n < num - 1; n++)
                                            //                        {
                                            //                            addCellLengthTop.Add(cellLength);
                                            //                            addCellTotalLengtTop = addCellTotalLengtTop + cellLength;
                                            //                        }

                                            //                        break;

                                            //                    }
                                            //                }

                                            //            }
                                            //            else
                                            //            {
                                            //                for (int n = 0; n < num; n++)
                                            //                {
                                            //                    addCellLengthTop.Add(cellLength);
                                            //                    addCellTotalLengtTop = addCellTotalLengtTop + cellLength;

                                            //                }
                                            //            }

                                            //        }
                                            //        else
                                            //        {
                                            //            continue;
                                            //        }

                                            //        #endregion

                                            //    }//   for (int num = 1; true; num++)结束


                                            //}
                                            #endregion


                                            break;

                                        }

                                        #endregion

                                    }

                                }// for (int i = 1; true; i++) 结束

                            }//       if (x1ToLeft > p1BaseSize * 0.5)结束



                            #endregion


                            #region 添加点的上侧Y方向坐标


                            if (addCellLengthTop.Count > 0)
                            {
                                int yCoorIndex = YCoord.Count - 1;

                                YCoord.Add(refinePoint1XYZ[1] + p1BaseSize * 0.5);

                                yCoorIndex = YCoord.Count - 1;

                                for (int i = 0; i < addCellLengthTop.Count; i++)
                                {
                                    YCoord.Add(YCoord[yCoorIndex] + addCellLengthTop[i]);

                                    yCoorIndex = yCoorIndex + 1;

                                }
                            }
                            else
                            {
                                YCoord.Add(upleft[1]);
                            }


                            #endregion
                        }



                        #endregion

                    }//Bias>1


                }
                //else
                //{
                //    //Bias<0

                //}


                #endregion

                return YCoord;

            }
            catch
            {
                YCoord = new List<double>();

                return YCoord;
            }

        }


        /// <summary>
        /// 返回 List<RefinePoint> SortPointXCoor 出错后返回的 Count=0
        /// 从小到大
        /// </summary>
        /// <param name="sourceRefinePoint"></param>
        /// <returns></returns>
        private static List<RefinePoint> SortPointXCoor(List<RefinePoint> sourceRefinePoint)
        {
            List<RefinePoint> SortPointXCoor = new List<RefinePoint>();

            try
            {



                if (sourceRefinePoint == null)
                {
                    return SortPointXCoor;
                }
                else
                {
                    if (sourceRefinePoint.Count <= 1)
                    {
                        return SortPointXCoor;
                    }
                    else
                    {
                        if (sourceRefinePoint.Count > 1)
                        {

                            //进行排序  因为数据量一般不会很大  可以选择冒泡排序法
                            //去掉 X 值相等 的点  只留一个
                            for (int i = 0; i < sourceRefinePoint.Count; i++)
                            {
                                RefinePoint refinePoint = new RefinePoint();
                                refinePoint = (RefinePoint)sourceRefinePoint[i].Clone();

                                bool has = false;

                                for (int j = 0; j < SortPointXCoor.Count ; j++)
                                {
                                    //如果相等
                                    if ( Math .Abs (  SortPointXCoor [j].RefinePointCoor .XCoordinate-refinePoint.RefinePointCoor .XCoordinate)<1e-6 )
                                    {
                                        has = true;
                                    }
                                }
                                if (has ==false )
                                {
                                     SortPointXCoor.Add(refinePoint);
                                }
                               
                            }

                            if (SortPointXCoor != null)
                            {
                                if (SortPointXCoor.Count >= 2)
                                {

                                    //冒泡排序 从小到大  大数后移的方法
                                    for (int i = 0; i < SortPointXCoor.Count; i++)
                                    {

                                        for (int j = 0; j < SortPointXCoor.Count - i - 1; j++)
                                        {
                                            if (SortPointXCoor[j].RefinePointCoor.XCoordinate > SortPointXCoor[j + 1].RefinePointCoor.XCoordinate)
                                            {

                                                //double temp = SortPointXCoor[j].RefinePointCoor.XCoordinate;
                                                //SortPointXCoor[j].RefinePointCoor.XCoordinate = SortPointXCoor[j + 1].RefinePointCoor.XCoordinate;
                                                //SortPointXCoor[j + 1].RefinePointCoor.XCoordinate = temp;

                                                RefinePoint tempPoint = new RefinePoint();
                                                tempPoint = (RefinePoint)SortPointXCoor[j].Clone();

                                                SortPointXCoor[j] = (RefinePoint )SortPointXCoor[j + 1].Clone();

                                                SortPointXCoor[j + 1] = (RefinePoint )tempPoint.Clone ();



                                            }


                                        }

                                    }



                                }

                            }

                        }

                    }


                }


                return SortPointXCoor;


            }
            catch
            {
                SortPointXCoor = new List<RefinePoint>();
                return SortPointXCoor;
            }

        }

        /// <summary>
        /// 返回 List<RefinePoint> SortPointYCoor 出错后返回的 Count=0
        /// 从小到大
        /// </summary>
        /// <param name="sourceRefinePoint"></param>
        /// <returns></returns>
        private static List<RefinePoint> SortPointYCoor(List<RefinePoint> sourceRefinePoint)
        {

            List<RefinePoint> SortPointYCoor = new List<RefinePoint>();


            try
            {



                if (sourceRefinePoint == null)
                {
                    return SortPointYCoor;
                }
                else
                {
                    if (sourceRefinePoint.Count <= 1)
                    {
                        return SortPointYCoor;
                    }
                    else
                    {
                        if (sourceRefinePoint.Count > 1)
                        {

                            //进行排序  因为数据量一般不会很大  可以选择冒泡排序法
                            //去掉 Y 值相等 的点  只留一个
                            for (int i = 0; i < sourceRefinePoint.Count; i++)
                            {
                                RefinePoint refinePoint = new RefinePoint();
                                refinePoint = (RefinePoint)sourceRefinePoint[i].Clone();

                                bool has = false;
                                for (int j = 0; j < SortPointYCoor.Count ; j++)
                                {
                                    //if (SortPointYCoor [j].RefinePointCoor .YCoordinate ==refinePoint .RefinePointCoor .YCoordinate )
                                    if (Math .Abs (  SortPointYCoor[j].RefinePointCoor.YCoordinate - refinePoint.RefinePointCoor.YCoordinate)<1e-6)
                                    {
                                        has = true;
                                    }
                                }
                                if (has ==false )
                                {
                                      SortPointYCoor.Add(refinePoint);
                                }
                             
                            }
                            if (SortPointYCoor != null)
                            {
                                if (SortPointYCoor.Count >= 2)
                                {

                                    //冒泡排序 从小到大  大数后移的方法
                                    for (int i = 0; i < SortPointYCoor.Count; i++)
                                    {

                                        for (int j = 0; j < SortPointYCoor.Count - i - 1; j++)
                                        {
                                            if (SortPointYCoor[j].RefinePointCoor.YCoordinate  > SortPointYCoor[j + 1].RefinePointCoor.YCoordinate )
                                            {

                                                //double temp = SortPointYCoor[j].RefinePointCoor.XCoordinate;
                                                //SortPointYCoor[j].RefinePointCoor.XCoordinate = SortPointYCoor[j + 1].RefinePointCoor.XCoordinate;
                                                //SortPointYCoor[j + 1].RefinePointCoor.XCoordinate = temp;


                                                RefinePoint tempPoint = new RefinePoint();
                                                tempPoint = (RefinePoint)SortPointYCoor[j].Clone();

                                                SortPointYCoor[j] = (RefinePoint)SortPointYCoor[j + 1].Clone();

                                                SortPointYCoor[j + 1] = (RefinePoint)tempPoint.Clone();

                                            }


                                        }

                                    }



                                }

                            }

                        }

                    }


                }


                return SortPointYCoor;



            }
            catch
            {
                SortPointYCoor = new List<RefinePoint>();

                return SortPointYCoor;
            }

        }




    }




}
